package cn.hinglo.website.controller;

import cn.hinglo.baseservice.annotation.IgnoreToken;
import cn.hinglo.baseservice.base.BaseController;
import cn.hinglo.common.entity.Result;
import cn.hinglo.website.bean.model.Article;
import cn.hinglo.website.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;


/**
 * @createTime: 2018/9/21 14:44
 * @author: HingLo
 * @description: 文章管理
 */
@RestController
@RequestMapping("/article")
@Validated
public class ArticleController extends BaseController {

    @Autowired
    private ArticleService articleService;

    /**
     * 通过文章的编号来获取文章具体内容
     *
     * @param id 文章的ID
     */
    @IgnoreToken
    @GetMapping("index")
    public Result index(@NotNull(message = "文章编号不能为空") Long id) {
        if (id == null) {
            return error("文章ID不能为空");
        } else {
            return articleService.indexService(id);
        }
    }

    /**
     * 添加文章内容
     *
     * @param article 文章对象
     */
    @PostMapping("add")
    public Result add(Article article, @RequestParam("img") MultipartFile img) {
        return articleService.addService(article, img);
    }

    /**
     * 删除指定的文章
     *
     * @param id 文章的ID
     */
    @DeleteMapping("delete")
    public Result delete(Long id) {
        if (id == null || id < 1) {
            error("文章ID不正确");
        }
        return result(articleService.deleteService(id));
    }

    /**
     * 前端启用状态的文章
     */
    @GetMapping("list")
    @IgnoreToken
    public Result list(Integer page, Integer size, @RequestParam("type") Integer type) {
        return success(articleService.listService(getPage(page), getSize(size), 1, type));
    }

    /**
     * 后台管理查看全部文章
     */
    @GetMapping("listAll")
    public Result listAll(Integer page, Integer size) {
        return success(articleService.listService(getPage(page), getSize(size), 0, 0));
    }

    /**
     * 修改文章启动状态
     *
     * @param id     文章ID
     * @param status 当前状态
     * @return 返回结果
     */
    @PutMapping("modifyStatus")
    public Result modifyStatus(Integer id, Integer status) {
        return result(articleService.modifyStatusService(id, status), "文章" + (status.equals(1) ? "启动" : "禁用") + "失败");
    }

}
